<?php
/**
 * CheckModel.class.php
 * 前导模型
 * 
 * @author  王中艺 <wangzy_smile@qq.com>
 * @date    2017-08-22
 */

namespace Wechat\Model;

class CheckModel extends BaseModel {

    protected   $trueTableName = 't_check';

    /**
     * 执行签到
     * @param   $userId
     * @return  boolean
     */
    function checkToday($userId){
        $today  = date('Y-m-d');

        //检测是否已签过
        $isExt  = $this->where('user_id = %d and date = "%s"', $userId, $today)->find();
        if($isExt){
            return false;
        }

        $checkInfo  = [
            'user_id'   => $userId,
            'date'  => $today,
            'keep_days' => 1,
            'create_time'   => time()
        ];

        //检测昨天签到信息
        $lastDay    = date('Y-m-d', strtotime('-1 days'));
        $lastCheck  = $this->where('user_id = %d and date = "%s"', $userId, $lastDay)->find();
        if($lastCheck){
            $checkInfo['keep_days'] = $lastCheck['keep_days'] + 1;
        }

        $checkId    = $this->add($checkInfo);
        return $checkId;
    }

    /**
     * 获取最近签到信息
     * @param   $userId
     * @return  $checkInfo
     */
    function getLastInfo($userId){
        $checkInfo  = $this->where('user_id = %d', $userId)->order('id desc')->cache(true)->find();
        return $checkInfo;
    }

}